{include file="common/head"}
<body>
<div class="weadmin-body">

    <div class="layui-row">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: -1px;">
            <legend>基本信息搜索</legend>
        </fieldset>
        <form class="layui-form layui-col-md12 we-search">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: 100px;">管理员名称:</label>
                <input type="text" name="username" id="username" placeholder="请输入管理员名称" autocomplete="off" class="layui-input">
            </div>
            <button class="layui-btn" type="button"  id="search" data-type="reload"><i class="layui-icon">&#xe615;</i>查询</button>
        </form>
    </div>
    <div class="weadmin-block" style="background-color: white;">
        <button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon">&#xe640;</i>批量删除</button>
        <button class="layui-btn" onclick="WeAdminShow('添加用户','{:url('Admin/add')}',800,600)"><i class="layui-icon">&#xe654;</i>添加</button>
    </div>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
        <legend>信息展示</legend>
    </fieldset>
    <div class="layui-row">
        <form class="layui-form">
            <table class="layui-table" id="admins" lay-filter="admins_data"></table>
            <script type="text/html" id="switchTpl">
                <!--自定义属性前需要加data-->
                <input type="checkbox" name="status" data-id="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="statusDemo" {{d.username == 'admin' ?'disabled':'' }} {{d.status == 1 ?'checked':'' }}>
            </script>
        </form>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"><i class="layui-icon">&#xe615;</i>查看</a>
            <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
        </script>
    </div>
</div>
<script type="text/javascript">
    layui.use(['layer', 'form','jquery','table'], function(){
        var table = layui.table,
            form = layui.form,
            layer=layui.layer,
            $= layui.$;
        form.render();
        form.on('select(role)', function(){
            var id = $('#role1').data('id');
          console.log(id);
        });
        table.render({
            elem: '#admins',
            id:'admins',
            limit:10,
            height: 473,
            url: "{:url('Admin/main')}", //数据接口
            page: true, //开启分页
            cols: [[ //表头
                {field: 'id', title: 'ID', width:'5%', sort: true, fixed: 'left'}
                ,{field: 'username', title: '用户名', width:'10%'}
                ,{field: 'nick_name', title: '昵称', width:'10%'}
                ,{field: 'create_time', title: '创建时间', width:'10%'}
                ,{field: 'status', title: '启用状态', width:'10%',templet: '#switchTpl', unresize: true}
                ,{field: 'last_login', title: '最后登录时间', width: '10%', sort: true}
                ,{field: 'group_id', title: '所属用户组', width: '10%', sort: true}
                ,{title: '操作',width:'20%',toolbar:'#barDemo',align:'center'}
            ]],
        });
       form.on('switch(statusDemo)', function(obj){
           var id=$(this).data('id');
            $.ajax({
                type:'post',
                dataType:"JSON",
                url:"{:url('Admin/statusChange')}",
                data:{id:id},
                success:function (res) {
                   if (res.code==200){
                       layer.msg(res['msg'],{icon:1,time:2000});
                       setTimeout(function () {
                           table.reload('admins', {
                               page: {
                                   curr: 1 //重新从第 1 页开始
                               }
                           });
                       },2000);
                   }else{
                       layer.msg(res['msg'],{icon:5});
                       setTimeout(function () {
                           self.location="{:url('Admin/main')}";
                       },3000);
                   }
                }
            })
        });
        table.on('tool(admins_data)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    id= data.id;
                    layer.close(index);
                    $.ajax({
                        url:"{:url('del')}",
                        type:"POST",
                        data:{id:id},
                        async:true,
                        success:function (res) {
                            if(res['status']==200){
                                layer.msg(res.msg,{icon:1,time:1000},function () {
                                    obj.del();
                                    var index = parent.layer.getFrameIndex(window.name);
                                    //关闭当前frame
                                    parent.layer.close(index);
                                    table.reload('admins', {
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    });
                                })
                            }else{
                                layer.msg(res.msg);
                            }
                        },
                        error:function () {
                            layer.msg('页面跳转失败!');
                        }
                    })
                });
            }
            else if(obj.event === 'edit'){
                layer.ready(function () {
                    layer.open({
                        type:2,
                        title:'编辑',
                        maxmin: false,
                        area:['65%','85%'],
                        content:"{:url('Admin/edit')}?id="+data.id,
                        cancel:function () {
                          self.location="{:url('Admin/main')}";//obj.update();
                        }
                    });
                });
            }
        });
        var $ = layui.$,active = {
            reload:function () {
                //var keyType=$("#key_type option:selected").val();
                var sex = $('input:radio[name="sex"]:checked').val();
                table.reload('admins',{
                    where:{
                        username:$("#username").val(),
                        sex:sex,
                        classify:$("#classify").val(),
                        limit: $('#limit').val()
                    }
                });
            }
        };
        $('#search').on('click',function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>